﻿Imports System.Reflection

<ComClass(ClsAcadBlock.ClassId, ClsAcadBlock.InterfaceId, ClsAcadBlock.EventsId)>
Public Class ClsAcadBlock

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Shadows Const ClassId As String = "b36a81b8-e71c-4dcd-9832-9b7e57946282"
    Public Shadows Const InterfaceId As String = "4ded4d12-798e-479c-ba9b-ae6666f61704"
    Public Shadows Const EventsId As String = "1dae6a3f-fbe0-4ea4-b50c-81511295085b"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub

    Private _Application As ClsAcadApplication
    Public Property Application() As ClsAcadApplication
        Get
            Return _Application
        End Get
        Set(ByVal value As ClsAcadApplication)
            _Application = value
        End Set
    End Property


    Private _Count As Integer
    Public Property Count() As Integer
        Get
            Return _Count
        End Get
        Set(ByVal value As Integer)
            _Count = value
        End Set
    End Property

    Private _Document As ClsAcadDocument
    Public Property Document() As ClsAcadDocument
        Get
            Return _Document
        End Get
        Set(ByVal value As ClsAcadDocument)
            _Document = value
        End Set
    End Property

    Private _Explodable As Boolean
    Public Property Explodable() As Boolean
        Get
            Return _Explodable
        End Get
        Set(ByVal value As Boolean)
            _Explodable = value
        End Set
    End Property

    Private _IsDynamicBlock As Boolean
    Public Property IsDynamicBlock() As Boolean
        Get
            Return _IsDynamicBlock
        End Get
        Set(ByVal value As Boolean)
            _IsDynamicBlock = value
        End Set
    End Property

    Private _ObjectName As String
    Public Property ObjectName() As String
        Get
            Return _ObjectName
        End Get
        Set(ByVal value As String)
            _ObjectName = value
        End Set
    End Property


    Private _ObjectID As Long
    Public Property ObjectID() As Long
        Get
            Return _ObjectID
        End Get
        Set(ByVal value As Long)
            _ObjectID = value
        End Set
    End Property

    Private _OwnerID As Long
    Public Property OwnerID() As Long
        Get
            Return _OwnerID
        End Get
        Set(ByVal value As Long)
            _OwnerID = value
        End Set
    End Property

    Private _Handle As String
    Public Property Handle() As String
        Get
            Return _Handle
        End Get
        Set(ByVal value As String)
            _Handle = value
        End Set
    End Property

    Private _IsLayout As Boolean
    Public Property IsLayout() As Boolean
        Get
            Return _IsLayout
        End Get
        Set(ByVal value As Boolean)
            _IsLayout = value
        End Set
    End Property

    Private _IsXRef As Boolean
    Public Property IsXRef() As Boolean
        Get
            Return _IsXRef
        End Get
        Set(ByVal value As Boolean)
            _IsXRef = value
        End Set
    End Property

    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _Origin As ClsPoint2d
    Public Property Origin() As ClsPoint2d
        Get
            Return _Origin
        End Get
        Set(ByVal value As ClsPoint2d)
            _Origin = value
        End Set
    End Property

    'Private _Path As String
    'Public Property Path() As String
    '    Get
    '        Return _Path
    '    End Get
    '    Set(ByVal value As String)
    '        _Path = value
    '    End Set
    'End Property

    Private _items As ClsAcadEntity()
    Public Property Items() As ClsAcadEntity()
        Get
            Return _items
        End Get
        Set(ByVal value As ClsAcadEntity())
            _items = value
        End Set
    End Property


    Public AcadObject As Object

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="acadBlock"></param>
    Public Sub CteateFromCad(acadBlock As Object, doc As ClsAcadDocument, app As ClsAcadApplication)
        If TypeName(acadBlock) = "IAcadBlock" Or TypeName(acadBlock) = "IAcadModelSpace" Or TypeName(acadBlock) = "IAcadPaperSpace" Then
            Me.AcadObject = acadBlock
            Dim t = Me.GetType()
            For Each item As PropertyInfo In t.GetProperties().ToList().Where(Function(c) c.CanWrite).ToList()
                If item.PropertyType.Namespace <> MethodBase.GetCurrentMethod().ReflectedType.Namespace Then
                    item.SetValue(CType(Me, Object), CallByName(acadBlock, item.Name, CallType.Get, Nothing), Nothing)
                Else
                    Me.Document = doc
                    Me.Application = app
                    Me.Origin = New ClsPoint2d() With {.X = acadBlock.Origin(0), .Y = acadBlock.Origin(1)}
                    Dim ims As New List(Of ClsAcadEntity)
                    For index = 0 To Me.AcadObject.Count - 1
                        ims.Add(Me.GetItem(index))
                    Next
                    Me.Items = ims.ToArray()
                End If
            Next
        End If
    End Sub

    Public Function GetItem(Index As Integer) As ClsAcadEntity
        Dim clsent As New ClsAcadEntity, acent As Object
        acent = Me.AcadObject.Item(Index)
        clsent.CteateFromCad(acent)
        Return clsent
    End Function
End Class


